import pandas as pd

df = pd.read_csv('static/data/info_clean.csv')


def level_boxplot():
    month_group = df[['month', '震级(M)']].groupby('month')
    name = [int(i[0]) for i in month_group]
    value = [[float(j[1]) for j in i[1].values] for i in month_group]
    return {
        'name': name,
        'value': value
    }


def month_line():
    df_line = df.groupby('month')['参考位置'].count().reset_index()
    return {
        'name': [i for i in df_line['month'].values.tolist()],
        'value': [int(i) for i in df_line['参考位置'].values.tolist()]
    }


def month_count_pie():
    df_pie = df.groupby('month')['参考位置'].count().reset_index()
    return {
        'data': [{'name': str(i[0]) + '月', 'value': i[1]} for i in df_pie[['month', '参考位置']].values.tolist()],
    }


def level_pie():
    return {
        'data': [{'value': len(df[(df['震级(M)'] > (i - 1) * 3) & (df['震级(M)'] <= i * 3)]),
                  'name': f'{(i - 1) * 3}-{i * 3}震级'} for i in range(1, 4)]
    }


def level_bar():
    df_bar = df.sort_values('震级(M)', ascending=False).head(10)
    return {
        'name': ['-'.join(i) for i in df_bar[['参考位置', '发震时刻']].values],
        'value': [float(i) for i in df_bar['震级(M)'].values]
    }


def deep_boxplot():
    month_group = df[['month', '深度(千米)']].groupby('month')
    name = [int(i[0]) for i in month_group]
    value = [[int(j[1]) for j in i[1].values] for i in month_group]
    return {
        'name': name,
        'value': value
    }
